package com.example.maili.woxiamg.adapter.homeadapter;


import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.maili.woxiamg.R;
import com.example.maili.woxiamg.activity.MoreNewActivity;
import com.example.maili.woxiamg.bean.HomeBean;
import com.example.maili.woxiamg.bean.HomeDataBean;
import com.example.maili.woxiamg.utils.AppUtils;
import com.example.maili.woxiamg.utils.OkHttpClientManager;
import com.example.maili.woxiamg.view.FullyGridLayoutManager;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Request;

/**
 * Created by MAILI on 2017/10/11.
 */

public class HomeTypeAdapter extends BaseMultiItemQuickAdapter<HomeDataBean, BaseViewHolder> implements View.OnClickListener {
    private Context context;
    private int page = 2;
    private List<HomeBean.NewsListBean> newsList = new ArrayList<>();
    private NewRecyclerViewAdapter newAdapter;
    private boolean flag = true;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    break;
            }
        }
    };
    private RecyclerView new_recycler;

    public HomeTypeAdapter(List<HomeDataBean> data, Context context) {
        super(data);
        this.context = context;
        addItemType(HomeDataBean.NEWS, R.layout.home_new_layout);
        addItemType(HomeDataBean.STORY, R.layout.home_story_layout);
        addItemType(HomeDataBean.SCRIPT, R.layout.home_script_layout);
        addItemType(HomeDataBean.VIDEO, R.layout.home_video_layout);
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeDataBean item) {
        switch (helper.getItemViewType()) {
            case HomeDataBean.NEWS://新闻
                newsList = item.getNewsList();
                View convertView = helper.getConvertView();
                new_recycler = (RecyclerView) convertView.findViewById(R.id.recycler_new);
                LinearLayout newMore = (LinearLayout) convertView.findViewById(R.id.linear_moreNew);
                LinearLayout newChange = (LinearLayout) convertView.findViewById(R.id.linear_changeNew);
                newMore.setOnClickListener(this);
                newChange.setOnClickListener(this);
                new_recycler.setLayoutManager(new FullyGridLayoutManager(context, 2, GridLayoutManager.HORIZONTAL, false));
                newAdapter = new NewRecyclerViewAdapter(R.layout.home_new_item, newsList, context);
                new_recycler.setAdapter(newAdapter);
                newAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        AppUtils.toast(context, "" + position);
                    }
                });
                break;
            case HomeDataBean.STORY://故事
                List<HomeBean.StoryListBean> storyList = item.getStoryList();
                View stroyView = helper.getConvertView();
                RecyclerView story_recycler = (RecyclerView) stroyView.findViewById(R.id.recycler_Story);
                LinearLayout storyMore = (LinearLayout) stroyView.findViewById(R.id.linear_moreStory);
                storyMore.setOnClickListener(this);
                story_recycler.setLayoutManager(new FullyGridLayoutManager(context, 2, GridLayoutManager.HORIZONTAL, false));
                StoryRecyclerViewAdapter storyAdapter = new StoryRecyclerViewAdapter(R.layout.home_new_item, storyList, context);
                story_recycler.setAdapter(storyAdapter);
                storyAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        AppUtils.toast(context, "" + position);
                    }
                });
                break;
            case HomeDataBean.SCRIPT://剧本
                List<HomeBean.ScriptListBean> scriptList = item.getScriptList();
                View scriptView = helper.getConvertView();
                RecyclerView script_recycler = (RecyclerView) scriptView.findViewById(R.id.recycler_Script);
                LinearLayout scriptMore = (LinearLayout) scriptView.findViewById(R.id.linear_moreScript);
                scriptMore.setOnClickListener(this);
                script_recycler.setLayoutManager(new FullyGridLayoutManager(context, 2, GridLayoutManager.HORIZONTAL, false));
                ScriptRecyclerViewAdapter scriptAdapter = new ScriptRecyclerViewAdapter(R.layout.home_new_item, scriptList, context);
                script_recycler.setAdapter(scriptAdapter);
                scriptAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        AppUtils.toast(context, "" + position);
                    }
                });
                break;
            case HomeDataBean.VIDEO://视频
                List<HomeBean.VideoListBean> videoList = item.getVideoList();
                View vedioView = helper.getConvertView();
                RecyclerView vedio_recycler = (RecyclerView) vedioView.findViewById(R.id.recycler_Video);
                LinearLayout vedioMore = (LinearLayout) vedioView.findViewById(R.id.linear_moreVedio);
                vedioMore.setOnClickListener(this);
                vedio_recycler.setLayoutManager(new FullyGridLayoutManager(context, 3, GridLayoutManager.HORIZONTAL, false));
                VedioRecyclerViewAdapter vedioAdapter = new VedioRecyclerViewAdapter(R.layout.home_recycler_vedio_item_layout, videoList, context);
                vedio_recycler.setAdapter(vedioAdapter);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.linear_moreNew://更多新闻
                AppUtils.toast(context, "新闻");
                context.startActivity(new Intent(context, MoreNewActivity.class));
                break;
            case R.id.linear_changeNew://新闻换一换
                AppUtils.toast(context, "新闻换一换");
                break;
            case R.id.linear_moreVedio:
                break;
            default:
                break;
        }
    }

    /**
     * 换一换数据请求
     *
     * @param
     * @param page
     */
    private void requestData(String url, int page) {
        OkHttpClientManager.getAsyn(url + page, new OkHttpClientManager.ResultCallback<String>() {
            @Override
            public void onError(Request request, Exception e) {
                newAdapter.loadMoreFail();//加载失败
            }

            @Override
            public void onResponse(String response) {
                Gson getgson = AppUtils.getgson();
                if (response == null && TextUtils.isEmpty(response)) {
                    newAdapter.loadMoreEnd();//加载结束
                    AppUtils.toast(context, "已无更多数据");
                    return;
                }
                HomeBean homeBean = getgson.fromJson(response, HomeBean.class);
                newsList.addAll(homeBean.getNewsList());
                newAdapter.notifyDataSetChanged();
                newAdapter.loadMoreComplete();//加载完成
            }
        });
    }
}
